home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 121_01 / more.c < prev    next >
Text File  |  1985-08-19  |  4KB  |  188 lines

  1. /*
  2. HEADER: CUG 121.??;
  3.  
  4.     TITLE:    more - list file(s) to terminal;
  5.     VERSION:    1.0;
  6.     DATE:    08/01/85;
  7.     DESCRIPTION: "This program lists one or more files, a page (or a
  8.         user-selectable amount) at a time, on the console.  The
  9.         names of the files to be listed are supplied as arguments
  10.         on the command line; if no arguments are given, the user
  11.         is prompted for filename(s).";
  12.     KEYWORDS:    list, page;
  13.     SYSTEM:    CP/M;
  14.     FILENAME:    MORE.C;
  15.     WARNINGS:    "Requires fseek.c for link.
  16.         The LERASE #define must be customised for the user's
  17.         terminal.";
  18.     AUTHORS:    Mike W. Meyer;
  19.     COMPILERS:    BDS-C 1.50;
  20. */
  21. /*
  22.     more - scan a file in strange increments...
  23.  
  24.     Description
  25.       This program takes zero or more arguments, each of which is the name
  26.     of a file (if no arguments are passed, the user is prompted for the
  27.     filenames - a null entry terminates the prompting).
  28.       For each file, the first page's worth of lines is listed and then
  29.     then the user is prompted with "more? ".  At this point, the following
  30.     replies are accepted:
  31.         N    stop listing file, go on to next
  32.      Y or (newline)    list next page
  33.         S    list next half page
  34.         0    go back to start of file, and list first page
  35.           nnn    list next nnn lines
  36.          -nnn    back up nnn lines, then list a page
  37. */
  38.  
  39. #include <bdscio.h>
  40. #include <hardware.h>
  41.  
  42. /* The following string is terminal-dependent & must be verified by the user */
  43. #define LERASE    "\027\015\036"    /* A string to back up a line, and erase it */
  44.  
  45. char *gets();
  46.  
  47. main(argc, argv)
  48. int argc;
  49. char **argv;
  50. {
  51. char buf[MAXLINE];
  52.  
  53.     if (argc == 2)
  54.         show(*++argv);
  55.     else if (argc > 2)
  56.         while (--argc) {
  57.             printf("hit newline to see %s",*++argv);
  58.             gets(buf);
  59.             show(*argv);
  60.         }
  61.     else
  62.         for (;;) {
  63.             puts("which file? ");
  64.             if (!*gets(buf))
  65.                 exit();
  66.             show(buf);
  67.         }
  68. }
  69. /*
  70.  * show - open the file, display it, and close it
  71.  */
  72. show(name)
  73. char *name;
  74. {
  75. char file[BUFSIZ];
  76.  
  77.     if (fopen(name, file) == -1) {
  78.         printf("can't open %s\n", name);
  79.         return;
  80.     }
  81.     process(file);
  82.     fclose(file);
  83. }
  84. /*
  85.  * process - go through a file page at a time, with or as
  86.  *    told by user
  87.  */
  88. process(file)
  89. char *file;
  90. {
  91. char buf[MAXLINE], c;
  92.  
  93.     page(file);
  94.     for (;;) {
  95.         puts("more? ");
  96.         if ((c = tolower(*gets(buf))) == 'n')
  97.             return;
  98.         if (!c || c == 'y') { 
  99.             if (page(file))
  100.                 return;
  101.         }
  102.         else if (c == 's') {
  103.             puts(LERASE);
  104.             if (lines(file, TLENGTH / 2))
  105.                 return;
  106.         }
  107.         else if (c == '0') {
  108.             fseek(file, 0, 0);
  109.             if (page(file))
  110.                 return;
  111.         }
  112.         else if (isdigit(c)) {
  113.             puts(LERASE);
  114.             if (lines(file, atoi(buf)))
  115.                 return;
  116.         }
  117.         else if (c == '-') {
  118.             backup(file, atoi(buf));
  119.             if (page(file))
  120.                 return;
  121.         }
  122.         else puts(LERASE);
  123.     }
  124. }
  125. /*
  126.  * page - output a screenful of data (returns TRUE iff EOF)
  127.  */
  128. page(file)
  129. char *file;
  130. {
  131.     puts(CLEARS);
  132.     return(lines(file, TLENGTH - 1));
  133. }
  134. /*
  135.  * lines - output count lines to the screen (returns TRUE iff EOF)
  136.  */
  137. lines(file, count)
  138. char *file;
  139. {
  140. int i, j, c;
  141.  
  142.     for (i = 0; i < count; i++) {
  143.         for (j = 0; j < TWIDTH; j++) {
  144.             if ((c = rdch(file)) == EOF)
  145.                 return(TRUE);
  146.             else if (c == '\t') {
  147.                 j = j / 8 * 8 + 8;
  148.                 if (j < TWIDTH)
  149.                     j--;
  150.                 else {
  151.                     putchar('\n');
  152.                     break;
  153.                 }
  154.             }
  155.             putchar(c);
  156.             if (c == '\n')
  157.                 break;
  158.         }
  159.     }
  160.     return(FALSE);
  161. }
  162. /*
  163.  * rdch - read a character, and take care of the CP/M kludges
  164.  */
  165. rdch(file)
  166. char *file;
  167. {
  168. int c;
  169.  
  170.     if ((c = getc(file)) == CPMEOF)
  171.         return(EOF);
  172.     if (c == '\r')
  173.         return(getc(file));
  174.     return(c);
  175. }
  176. /*
  177.  * backup - by count lines (or guess at it), and then get to
  178.  *    a line boundary
  179.  */
  180. backup(file, count)
  181. char *file;
  182. {
  183. char buf[MAXLINE];
  184.  
  185.     fseek(file, count / 3, 4);
  186.     fgets(buf, file);
  187. }
  188.